﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OctoOS.UI;
using OctoOS;

public partial class root_admin_adminDefault : OOSPage
{
    private Resource _resource = null;

    protected void Page_Init(object sender, EventArgs e)
    {
#warning admin
        //OOSRequest.Current.EnsureAdmin();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        _resource = DataSource.ClassifiedResources_Get(
           OOSRequest.Current.ResourceID);

       PageTitle = "Classifieds (Admin)";

       SetBreadcrumbsAndActions();

       if (_resource != null)
       {
           Helper.Categories.UpdateCategoryCache();

           Category[] allCategories = Helper.Categories.GetAllCategories();

           foreach (var cat in allCategories.Where(f => f.ParentCategoryID == 0))
           {
               TextTreeNode rootNode = new TextTreeNode(cat.Name);
               if (!_resource.ExistsMasterResource)
               {
                   rootNode.HRef = Urls.Classifieds.GetCategoryEdit(cat.ID);
               }
               foreach (var childCat in allCategories.Where(f => f.ParentCategoryID == cat.ID))
               {
                   TextTreeNode childNode = new TextTreeNode(childCat.Name);
                   if (!_resource.ExistsMasterResource)
                   {
                       childNode.HRef = Urls.Classifieds.GetCategoryEdit(childCat.ID);
                   }
                   foreach (var grandChildCat in allCategories.Where(f => f.ParentCategoryID == childCat.ID))
                   {
                       TextTreeNode grandChildNode = new TextTreeNode(grandChildCat.Name);
                       if (!_resource.ExistsMasterResource)
                       {
                           grandChildNode.HRef = Urls.Classifieds.GetCategoryEdit(grandChildCat.ID);
                       }
                       childNode.Nodes.Add(grandChildNode);
                   }
                   rootNode.Nodes.Add(childNode);
               }
               categoryTree.Nodes.Add(rootNode);
           }
       }
    }

    private void SetBreadcrumbsAndActions()
    {
        AddBreadcrumb(string.Empty, "/");
        if (_resource != null)
        {
            if (!_resource.ExistsMasterResource)
            {
                AddAction("Add New Category", Urls.Classifieds.GetCategoryEdit(0));
            }
            AddAction("Ads to approve", Urls.Classifieds.GetAdsToApproveList(0));
        }
        AddAction("Settings", Urls.Classifieds.GetSettings());
    }
}